import { fileURLToPath, URL } from 'node:url'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import UnoCSS from 'unocss/vite'
import { resolve } from 'node:path'
import AutoImport from 'unplugin-auto-import/vite'
import { FileSystemIconLoader } from 'unplugin-icons/loaders'
import { webUpdateNotice } from '@plugin-web-update-notification/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'
import { ArcoResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig, loadEnv } from 'vite'
import { generateApiSchema } from './config/plugins/vitePluginGenerateApiSchema'
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, 'env', '')
  return {
    envDir: resolve(__dirname, './env'),
    plugins: [
      vue(),
      vueJsx(),
      UnoCSS(),
      webUpdateNotice({
        hiddenDefaultNotification: true,
        hiddenDismissButton: true,
        versionType: 'git_commit_hash'
      }),
      generateApiSchema({
        conf: {
          base: 'http://cdn-swagger.dev.com/',
          services: [{ name: 'all', url: '/configs/srv-auth.swagger.json' }]
        }
      }),
      AutoImport({
        dts: './typings/auto-imports.d.ts',
        vueTemplate: true,
        resolvers: [ArcoResolver({ importStyle: false })],
        dirs: ['./src/utils/*.ts', './src/hooks/*.ts', './src/constants/*.ts'],
        imports: ['vue', 'vue-router', 'pinia']
      }),
      Components({
        dts: './typings/components.d.ts',
        dirs: ['./src/components'],
        resolvers: [
          IconsResolver({
            customCollections: ['icons']
          }),
          ArcoResolver({
            importStyle: false,
            sideEffect: true
          })
        ]
      }),
      Icons({
        autoInstall: true,
        compiler: 'vue3',
        jsx: 'react',
        customCollections: {
          icons: FileSystemIconLoader('./src/assets/icons')
        }
      })
    ],
    base: env.VITE_PREFIX || '/admin/',
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    }
  }
})
